package com.eternal.base.database.dao;

import androidx.paging.DataSource;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;

import com.eternal.base.concat.TmpHum;
import com.eternal.base.database.entity.History;

import java.util.List;

@Dao
public interface HistoryDao {

    @Query("select time, tmp, hum, off, portStateList from history where mac=:mac order by time desc")
    DataSource.Factory<Integer, TmpHum> loadAllHistoryFromMac(String mac);

    @Query("select time from history where mac=:mac order by time desc limit 1")
    int loadLastTime(String mac);

    @Query("select time, tmp, hum, off, portStateList from history where mac=:mac and strftime('%M', datetime(time, 'unixepoch','localtime')) in (:times) " +
            "and time>:start and time<:end")
    DataSource.Factory<Integer, TmpHum> loadAllHistoryFromMacMin(String mac, List<String> times, long start, long end);

    @Query("select time, tmp, hum, off, portStateList from history where mac=:mac and strftime('%H', datetime(time, 'unixepoch','localtime')) in (:hours)" +
            "and strftime('%M', datetime(time, 'unixepoch','localtime'))=:min and time>:start and time<:end")
    DataSource.Factory<Integer, TmpHum> loadAllHistoryFromMacHour(String mac, List<String> hours, String min, long start, long end);

    @Query("select time, tmp, hum, off, portStateList from history where mac=:mac and time>:start and time<:end")
    DataSource.Factory<Integer, TmpHum> loadAllHistoryFromMac(String mac, long start, long end);

    @Insert
    void insert(History history);

    @Query("select time from history where mac=:mac order by time asc limit 1")
    int loadFirstTime(String mac);
}
